package com.cms.manage.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;

public class LogFileUtil {
	
	
	/**通过文本方式写日志,当前文件名存在添加
	 * 不存在创建新的文件
	 * logFileName文件名
	 * content 文件内容
	 * path 文件相对路径
	 * type 后缀名
	 * */
	public static void  writeLogByText(String logFileName,String content,String path,String type){
		File file =new File(System.getProperty("user.dir"));  
		//获取服务器根目录
		Boolean flag = true;
		while(flag){
			if(file.getParentFile()==null){
				flag=false;
			}else{
				file=file.getParentFile();
			}
		}
		String basePath = file.getPath()+System.getProperty("file.separator")+"himew"+System.getProperty("file.separator")+"www"+System.getProperty("file.separator")+"logs"+System.getProperty("file.separator")+path;
		file =new File(basePath); 
		//判断文件目录是否存在否则创建
		if(!file.exists()){
			file.mkdirs();	
		}
		String logPath = basePath+System.getProperty("file.separator")+logFileName+"."+type;
	    file = new File(logPath);
	    FileWriter fw = null;
	    try { 
		if(!file.exists())    
		{    		     
		    file.createNewFile();  		        
		} 
		 fw = new FileWriter(file,true);
		 fw.write(DateTimeUtil.getDateYMDHMS(new Date())+"\r\n");
         fw.write(content);//向文件中写内容
         fw.write("==============================================================\r\n");
         fw.flush();         
	 } catch (IOException e) {    		         
	        e.printStackTrace();    
	  } finally {
		  if(fw != null){
              try {
                  fw.close();
              } catch (IOException e) {               
                  e.printStackTrace();
              }
          }

		}
	}
	/**
	 * 支付日志
	 * 
	 * */
	  public static void addPayLog(Map<String, String> data){
		  StringBuffer contentBf = new StringBuffer();
		  data.forEach((k,v)->{
			  if("pass_key".equals(k)||"verify_key".equals(k)){
				  contentBf.append("");
				}else{
					contentBf.append(k+"===="+v+"\r\n");
				}			  
		  });
		  String path = System.getProperty("file.separator")+"logs"+System.getProperty("file.separator")+"iosverify";
		  String type = "txt";
		  String logFileName=DateTimeUtil.getDateYM(new Date())+"_"+data.get("userName")+"_"+data.get("pengyou");
		  LogFileUtil.writeLogByText(logFileName,contentBf.toString(),path,type); 
	  }	
   public static void main(String[] args) {

	   System.out.println(DateTimeUtil.getDateYMDHMS(new Date()));
	   LogFileUtil.writeLogByText(DateTimeUtil.getDateYM(new Date())+"_"+"ceshi"+"_"+"ceshi12", "1", System.getProperty("file.separator")+"logss"+System.getProperty("file.separator")+"iosverify","txt");
	   
}
}
